home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.001 / tcpdump-~ / tcpdump-3.0.2-linux / tcpdump-3.0.2 / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-30  |  6.4 KB  |  342 lines

  1. /*
  2.  * Copyright (c) 1990, 1991, 1993, 1994, 1995
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21.  
  22. #ifndef lint
  23. static char rcsid[] =
  24.     "@(#) $Header: util.c,v 1.28+ 94/06/12 14:30:31 leres Exp $ (LBL)";
  25. #endif
  26.  
  27. #include <stdlib.h>
  28. #include <sys/types.h>
  29. #include <sys/time.h>
  30. #include <sys/file.h>
  31. #include <sys/stat.h>
  32.  
  33. #include <ctype.h>
  34. #ifdef SVR4
  35. #include <fcntl.h>
  36. #endif
  37. #ifdef __STDC__
  38. #include <stdlib.h>
  39. #endif
  40. #include <stdio.h>
  41. #if __STDC__
  42. #include <stdarg.h>
  43. #else
  44. #include <varargs.h>
  45. #endif
  46. #include <string.h>
  47. #include <unistd.h>
  48.  
  49. #include "interface.h"
  50.  
  51. /*
  52.  * Print out a filename (or other ascii string).
  53.  * If ep is NULL, assume no truncation check is needed.
  54.  * Return true if truncated.
  55.  */
  56. int
  57. fn_print(register const u_char *s, register const u_char *ep)
  58. {
  59.     register int ret;
  60.     register u_char c;
  61.  
  62.     ret = 1;            /* assume truncated */
  63.     putchar('"');
  64.     while (ep == NULL || s < ep) {
  65.         c = *s++;
  66.         if (c == '\0') {
  67.             ret = 0;
  68.             break;
  69.         }
  70.         if (!isascii(c)) {
  71.             c = toascii(c);
  72.             putchar('M');
  73.             putchar('-');
  74.         }
  75.         if (!isprint(c)) {
  76.             c ^= 0x40;    /* DEL to ?, others to alpha */
  77.             putchar('^');
  78.         }
  79.         putchar(c);
  80.     }
  81.     putchar('"');
  82.     return(ret);
  83. }
  84.  
  85. /*
  86.  * Print out a counted filename (or other ascii string).
  87.  * If ep is NULL, assume no truncation check is needed.
  88.  * Return true if truncated.
  89.  */
  90. int
  91. fn_printn(register const u_char *s, register u_int n,
  92.       register const u_char *ep)
  93. {
  94.     register int ret;
  95.     register u_char c;
  96.  
  97.     ret = 1;            /* assume truncated */
  98.     putchar('"');
  99.     while (ep == NULL || s < ep) {
  100.         if (n-- <= 0) {
  101.             ret = 0;
  102.             break;
  103.         }
  104.         c = *s++;
  105.         if (!isascii(c)) {
  106.             c = toascii(c);
  107.             putchar('M');
  108.             putchar('-');
  109.         }
  110.         if (!isprint(c)) {
  111.             c ^= 0x40;    /* DEL to ?, others to alpha */
  112.             putchar('^');
  113.         }
  114.         putchar(c);
  115.     }
  116.     putchar('"');
  117.     return(ret);
  118. }
  119.  
  120. /*
  121.  * Print the timestamp
  122.  */
  123. void
  124. ts_print(register const struct timeval *tvp)
  125. {
  126.     register int s;
  127.     extern int32 thiszone;
  128.  
  129.     if (tflag > 0) {
  130.         /* Default */
  131.         s = (tvp->tv_sec + thiszone) % 86400;
  132.         (void)printf("%02d:%02d:%02d.%06d ",
  133.             s / 3600, (s % 3600) / 60, s % 60, tvp->tv_usec);
  134.     } else if (tflag < 0) {
  135.         /* Unix timeval style */
  136.         (void)printf("%d.%06d ", tvp->tv_sec, tvp->tv_usec);
  137.     }
  138. }
  139.  
  140. /*
  141.  * Convert a token value to a string; use "fmt" if not found.
  142.  */
  143. const char *
  144. tok2str(register const struct token *lp, register const char *fmt,
  145.     register int v)
  146. {
  147.     static char buf[128];
  148.  
  149.     while (lp->s != NULL) {
  150.         if (lp->v == v)
  151.             return (lp->s);
  152.         ++lp;
  153.     }
  154.     if (fmt == NULL)
  155.         fmt = "#%d";
  156.     (void)sprintf(buf, fmt, v);
  157.     return (buf);
  158. }
  159.  
  160. /* A replacement for strdup() that cuts down on malloc() overhead */
  161. char *
  162. savestr(register const char *str)
  163. {
  164.     register u_int size;
  165.     register char *p;
  166.     static char *strptr = NULL;
  167.     static u_int strsize = 0;
  168.  
  169.     size = strlen(str) + 1;
  170.     if (size > strsize) {
  171.         strsize = 1024;
  172.         if (strsize < size)
  173.             strsize = size;
  174.         strptr = (char *)malloc(strsize);
  175.         if (strptr == NULL)
  176.             error("savestr: malloc");
  177.     }
  178.     (void)strcpy(strptr, str);
  179.     p = strptr;
  180.     strptr += size;
  181.     strsize -= size;
  182.     return (p);
  183. }
  184.  
  185. #ifdef NOVFPRINTF
  186. /*
  187.  * Stock 4.3 doesn't have vfprintf.
  188.  * This routine is due to Chris Torek.
  189.  */
  190. vfprintf(f, fmt, args)
  191.     FILE *f;
  192.     char *fmt;
  193.     va_list args;
  194. {
  195.     int ret;
  196.  
  197.     if ((f->_flag & _IOWRT) == 0) {
  198.         if (f->_flag & _IORW)
  199.             f->_flag |= _IOWRT;
  200.         else
  201.             return EOF;
  202.     }
  203.     ret = _doprnt(fmt, args, f);
  204.     return ferror(f) ? EOF : ret;
  205. }
  206. #endif
  207.  
  208. /* VARARGS */
  209. __dead void
  210. #if __STDC__ || defined(SOLARIS)
  211. error(char *fmt, ...)
  212. #else
  213. error(fmt, va_alist)
  214.     char *fmt;
  215.     va_dcl
  216. #endif
  217. {
  218.     va_list ap;
  219.  
  220.     (void)fprintf(stderr, "%s: ", program_name);
  221. #if __STDC__
  222.     va_start(ap, fmt);
  223. #else
  224.     va_start(ap);
  225. #endif
  226.     (void)vfprintf(stderr, fmt, ap);
  227.     va_end(ap);
  228.     if (*fmt) {
  229.         fmt += strlen(fmt);
  230.         if (fmt[-1] != '\n')
  231.             (void)fputc('\n', stderr);
  232.     }
  233.     exit(1);
  234.     /* NOTREACHED */
  235. }
  236.  
  237. /* VARARGS */
  238. void
  239. #if __STDC__ || defined(SOLARIS)
  240. warning(char *fmt, ...)
  241. #else
  242. warning(fmt, va_alist)
  243.     char *fmt;
  244.     va_dcl
  245. #endif
  246. {
  247.     va_list ap;
  248.  
  249.     (void)fprintf(stderr, "%s: warning: ", program_name);
  250. #if __STDC__
  251.     va_start(ap, fmt);
  252. #else
  253.     va_start(ap);
  254. #endif
  255.     (void)vfprintf(stderr, fmt, ap);
  256.     va_end(ap);
  257.     if (*fmt) {
  258.         fmt += strlen(fmt);
  259.         if (fmt[-1] != '\n')
  260.             (void)fputc('\n', stderr);
  261.     }
  262. }
  263.  
  264. /*
  265.  * Copy arg vector into a new buffer, concatenating arguments with spaces.
  266.  */
  267. char *
  268. copy_argv(register char **argv)
  269. {
  270.     register char **p;
  271.     register int len = 0;
  272.     char *buf;
  273.     char *src, *dst;
  274.  
  275.     p = argv;
  276.     if (*p == 0)
  277.         return 0;
  278.  
  279.     while (*p)
  280.         len += strlen(*p++) + 1;
  281.  
  282.     buf = (char *)malloc(len);
  283.  
  284.     p = argv;
  285.     dst = buf;
  286.     while ((src = *p++) != NULL) {
  287.         while ((*dst++ = *src++) != '\0')
  288.             ;
  289.         dst[-1] = ' ';
  290.     }
  291.     dst[-1] = '\0';
  292.  
  293.     return buf;
  294. }
  295.  
  296. char *
  297. read_infile(char *fname)
  298. {
  299.     struct stat buf;
  300.     int fd;
  301.     char *p;
  302.  
  303.     fd = open(fname, O_RDONLY);
  304.     if (fd < 0)
  305.         error("can't open '%s'", fname);
  306.  
  307.     if (fstat(fd, &buf) < 0)
  308.         error("can't state '%s'", fname);
  309.  
  310.     p = (char *)malloc((u_int)buf.st_size);
  311.     if (read(fd, p, (int)buf.st_size) != buf.st_size)
  312.         error("problem reading '%s'", fname);
  313.  
  314.     return p;
  315. }
  316.  
  317. int
  318. gmt2local()
  319. {
  320. #if !defined(SVR4) && !defined(linux)
  321.     struct timeval tv;
  322.     struct timezone tz;
  323.     register struct tm *tm;
  324.     register int t;
  325.  
  326.     if (gettimeofday(&tv, &tz) < 0)
  327.         error("gettimeofday");
  328.     tm = localtime((time_t *)&tv.tv_sec);
  329. #ifndef SUNOS3
  330.     t = tm->tm_gmtoff;
  331. #else
  332.     t = tz.tz_minuteswest * -60;
  333.     if (tm->tm_isdst)
  334.         t += 60 * 60;
  335. #endif
  336.     return (t);
  337. #else
  338.     tzset();
  339.     return (-altzone);
  340. #endif
  341. }
  342.